<?php
/**
 * Created by IntelliJ IDEA.
 * User: wuzesheng
 * Date: 4/15/14
 * Time: 5:28 PM
 */
ini_set('max_execution_time', '0');
ini_set('memory_limit', '6000M');
require(__DIR__ . "/../bootstrap.php");

// Construct the FDS client
$access_key = "5411744243597";
$access_secret = "hu/XTi7mhl1j+pL5qkQGnw==";
$credential = new \FDS\credential\BasicFDSCredential($access_key, $access_secret);
$fds_config = new \FDS\FDSClientConfiguration();
$fds_config->setRegionName("awsbj0");
$fds_config->enableHttps(true);
$fds_config->enableCdnForUpload(false);
$fds_config->enableCdnForDownload(true);

$fds_client = new \FDS\GalaxyFDSClient($credential, $fds_config);
$bucket_name = "madv360";

// Check and create the bucket
if (!$fds_client->doesBucketExist($bucket_name)) {
  $fds_client->createBucket($bucket_name);
}

require_once '/mnt/default/crontabscript/examples/EasyMySQLi.inc.php';
$db = new EasyMySQLi('localhost', 'root', 'root', 'madv_db1');
try {
  $result=$db->queryAllRows('select id,filename,type from file');
  if(!empty($result)){
    for($i=0;$i<count($result);$i++){
      if(!empty($result[$i]["filename"]) && empty($result[$i]["md5"]) && empty($result[$i]["filesize"])){
        $object_name=$result[$i]["filename"].(($result[$i]["type"]==1)?".mp4":".png");
        $exist = $fds_client->doesObjectExist($bucket_name, $object_name);
        if($exist){
          $metadata_object = $fds_client->getObjectMetadata($bucket_name, $object_name);
          $metadata_content = $metadata_object->getRawMetadata();
          //print_r($metadata_content);
          $db->queryNoResult('update file set md5="'.$metadata_content["content-md5"].'",filesize="'.$metadata_content["x-xiaomi-meta-content-length"].'" where id=?',$result[$i]["id"]);
        }
      }
    }
  }
  $result=$db->queryAllRows('select * from file_transcode');
  if(!empty($result)){
    for($i=0;$i<count($result);$i++){
      if(!empty($result[$i]["filename"]) && empty($result[$i]["md5"]) && empty($result[$i]["filesize"])){
        $object_name=$result[$i]['filename']."_".$result[$i]['resolution'].'_'.$result[$i]['videocodec'].'_'.$result[$i]['bitrate'].'.mp4';
        $exist = $fds_client->doesObjectExist($bucket_name, $object_name);
        if($exist){
          $metadata_object = $fds_client->getObjectMetadata($bucket_name, $object_name);
          $metadata_content = $metadata_object->getRawMetadata();
          $db->queryNoResult('update file_transcode set md5="'.$metadata_content["content-md5"].'",filesize="'.$metadata_content["x-xiaomi-meta-content-length"].'" where id=?',$result[$i]["id"]);
        }
      }
    }
  }
}catch (MySQLiQueryException $ex) {
  //echo '{"rval":3}';// 失败
}

// Put an object
//$object_name = "test.txt";
//$result = $fds_client->getObjectMetadata($bucket_name, $object_name);
//print_r($result->getRawMetadata());
